package com.csi.bumblebee.controller.backstage;

import com.csi.bumblebee.domain.user.Company;
import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.domain.util.StateCode;
import com.csi.bumblebee.service.CompanyService;
import com.csi.bumblebee.util.PageHelp;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.csi.bumblebee.domain.util.StateCode.FIND_FAILURE;
import static com.csi.bumblebee.domain.util.StateCode.FIND_SUCCESS;

/**
 * @author 孙伟俊
 */
@RestController
@RequestMapping("/backstage/company")
public class BackStageCompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 展示所有通过的企业(不包括登录名和密码)
     */
    @PostMapping("/listPassedCompany")
    public MyResult<PageInfo<Company>> list(@RequestBody PageHelp pageHelp){

        PageInfo<Company> list = companyService.list(pageHelp.getCurrentPage(), pageHelp.getPerPageSize());

        MyResult<PageInfo<Company>> myResult = new MyResult<>();
        myResult.setData(list);
        myResult.setCode(StateCode.FIND_SUCCESS);

        return myResult;
    }

    /**
     * 展示所有在申请的企业
     */
    @GetMapping("/apply")
    public MyResult<List<Company>> listAllApplyCompany(){

        MyResult<List<Company>> myResult = new MyResult<>();

        List<Company> companyList = companyService.listApplyCompany();
        if (companyList != null){
            myResult.setCode(FIND_SUCCESS);
            myResult.setData(companyList);
            myResult.setMessage("查询成功");
        }else {
            myResult.setCode(FIND_FAILURE);
            myResult.setMessage("查询失败");
        }
        return myResult;
    }

    /**
     * 查找单个企业
     */
    @GetMapping("/{id}")
    public MyResult<Company>  findById(@PathVariable String id){
        MyResult<Company> myResult =new MyResult<>();
        Company company = companyService.findById(id);
        if (company!=null){
            myResult.setCode(FIND_SUCCESS);
            myResult.setData(company);
            myResult.setMessage("查询成功");
            return myResult;
        }else {
            myResult.setCode(FIND_FAILURE);
            myResult.setMessage("查询失败");
            return myResult;
        }
    }

    /**
     * 新增企业
     */
    @PostMapping("/company")
    public MyResult<Integer> add(@RequestBody Company company){

        MyResult<Integer> myResult = new MyResult<>();

        int result = companyService.add(company);

        System.out.println(company);

        if (result > 0){
            myResult.setMessage("新增成功");
            myResult.setCode(StateCode.INSERT_SUCCESS);
        }else {
            myResult.setMessage("新增失败");
            myResult.setCode(StateCode.INSERT_FAILURE);
        }
        return myResult;
    }

    /**
     * 更新企业信息(不包括账号)
     */
    @PutMapping("/company")
    public MyResult<Integer> update(@RequestBody Company company){

        MyResult<Integer> myResult = new MyResult<>();

        int result = companyService.update(company);
        if (result > 0){
            myResult.setCode(StateCode.UPDATE_SUCCESS);
            myResult.setMessage("更新成功");
        }else {
            myResult.setCode(StateCode.UPDATE_FAILURE);
            myResult.setMessage("更新失败");
        }

        return myResult;
    }

    /**
     * 根据Id删除某个企业
     */
    @DeleteMapping("/{id}")
    public MyResult<Integer> delete(@PathVariable String id){

        MyResult<Integer> myResult = new MyResult<>();

        int result = companyService.delete(id);
        if (result > 0) {
            myResult.setCode(StateCode.DELETE_SUCCESS);
            myResult.setMessage("删除成功");
        }else {
            myResult.setCode(StateCode.DELETE_FAILURE);
            myResult.setMessage("删除失败");
        }
        return myResult;
    }

    /**
     * 企业账号审核通过
     */
    @PutMapping("/updatePass/{companyId}/{status}")
    public MyResult<Integer> updatePass(@PathVariable String companyId, @PathVariable int status){
        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "更新成功");

        Map<String, Object> map = new HashMap<>();
        map.put("id", companyId);
        map.put("status", status);

        int result = companyService.updatePass(map);
        if (result > 0) {
            return myResult;
        }else {
            myResult.setMessage("更新失败");
            myResult.setCode(StateCode.UPDATE_FAILURE);
        }
        return myResult;
    }

    /**
     * 企业审核未通过
     */
    @PutMapping("/updateNotPass/{companyId}/{status}")
    public MyResult<Integer> updateNotPass(@PathVariable String companyId, @PathVariable int status){
        MyResult<Integer> myResult = new MyResult<>(StateCode.UPDATE_SUCCESS, "更新成功");

        Map<String, Object> map = new HashMap<>();
        map.put("id", companyId);
        map.put("status", status);

        int result = companyService.updateNotPass(map);
        if (result > 0) {
            return myResult;
        }else {
            myResult.setMessage("更新失败");
            myResult.setCode(StateCode.UPDATE_FAILURE);
        }
        return myResult;
    }

}
